//
//  PersonalProfileViewModel.m
//  ChatForHot
//
//  Created by wr on 2018/10/12.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalProfileViewModel.h"

@interface PersonalProfileViewModel ()

@property (nonatomic, strong, readwrite) NSMutableArray <PersonalCellModel *> *cellDataSource;
@property (nonatomic, strong, readwrite) NSMutableArray <PersonalCellModel *> *bottomCellDataSource;

@property (nonatomic, strong, readwrite) RACCommand *requestUserInfoCommand;
@property (nonatomic, strong, readwrite) RACCommand *cencelRequestUserInfoCommand;

@property (nonatomic, strong, readwrite) RACCommand *requestSaveUserInfoCommand;
@property (nonatomic, strong, readwrite) RACCommand *cencelRequestSaveUserInfoCommand;

@property (nonatomic, strong, readwrite) UserInfoModel *model;
@end

@implementation PersonalProfileViewModel

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    
    @weakify(self)
    self.cencelRequestUserInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
    
    self.requestUserInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            [dic setValue:[APPINFO defaultManager].token forKey:@"token"];
            [NetWorkTool HOTCHATRequestWithStringURL:ThebyTokenGetAccountApi progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"POST" Parma:dic SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                @strongify(self)
                if (isSucess) {
                    self.model = [[UserInfoModel alloc] init];
                    [self.model setValuesForKeysWithDictionary:resultDictionary[@"data"]];
                    [self.model HOTCHATsaveTheUserInfo];
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.cencelRequestUserInfoCommand.executionSignals];
    }];
    
    self.cencelRequestSaveUserInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
    
    self.requestSaveUserInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *input) {
        return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [NetWorkTool HOTCHATRequestWithStringURL:UPDATEAccountINFO_URL progress:^(NSProgress * _Nonnull progress) {
            } RequestWay:@"POST" Parma:input SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
                if (isSucess) {
                    [NetWorkTool HOTCHATUpdateToken];
                    [subscriber sendNext:nil];
                }
                [subscriber sendCompleted];
            } FailureBlock:^(NSError * _Nonnull error) {
                [subscriber sendError:error];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }] materialize] takeUntil:self.cencelRequestSaveUserInfoCommand.executionSignals];
    }];
}

-(NSMutableArray<PersonalCellModel *> *)cellDataSource {
    NSMutableArray *cellDataSource = [[NSMutableArray alloc] init];
    PersonalCellModel *age = [[PersonalCellModel alloc] init];
    age.title = NSLocalizedString(@"profile1", nil);
    age.icon = @"all_personal_age";
    age.info = [APPINFO defaultManager].age;
    age.tag = 0;
    age.thinLineIsHidden = NO;
    age.thickLineIsHidden = YES;
    age.cellHeight = 44;
    age.handler = ^{
    };
    
    PersonalCellModel *Height = [[PersonalCellModel alloc] init];
    Height.title = NSLocalizedString(@"profile2", nil);
    Height.icon = @"all_personal_height";
    Height.info = [self HOTCHATengUserHeightWith:[APPINFO defaultManager].maxFeet minFeet:[APPINFO defaultManager].minFeet];
    Height.tag = 1;
    Height.thinLineIsHidden = NO;
    Height.thickLineIsHidden = YES;
    Height.cellHeight = 44;
    Height.handler = ^{
    };
    
    PersonalCellModel *type = [[PersonalCellModel alloc] init];
    type.title = NSLocalizedString(@"profile3", nil);
    type.icon = @"all_personal_type";
    type.info = [UserInfoModel HOTCHATgetTypeNameWith:[APPINFO defaultManager].weight];
    type.tag = 2;
    type.thinLineIsHidden = NO;
    type.thickLineIsHidden = YES;
    type.cellHeight = 44;
    type.handler = ^{
    };
    
    PersonalCellModel *constellation = [[PersonalCellModel alloc] init];
    constellation.title = NSLocalizedString(@"profile4", nil);
    constellation.icon = @"all_personal_constellation";
    constellation.info = [UserInfoModel HOTCHATgetConstellationNameWithID:[APPINFO defaultManager].constellation];
    constellation.tag = 3;
    constellation.thinLineIsHidden = NO;
    constellation.thickLineIsHidden = YES;
    constellation.cellHeight = 44;
    constellation.handler = ^{
    };
    
    PersonalCellModel *income = [[PersonalCellModel alloc] init];
    income.title = NSLocalizedString(@"profile5", nil);
    income.icon = @"all_personal_income";
    income.info = [UserInfoModel HOTCHATgetIncomeNameWithID:[APPINFO defaultManager].income];
    income.tag = 4;
    income.thinLineIsHidden = NO;
    income.thickLineIsHidden = YES;
    income.cellHeight = 44;
    income.handler = ^{
        
        
    };
    
    PersonalCellModel *job = [[PersonalCellModel alloc] init];
    job.title = NSLocalizedString(@"profile6", nil);
    job.icon = @"all_personal_job";
    job.info = [UserInfoModel HOTCHATgetJobNameWithID:[APPINFO defaultManager].occupation];
    job.tag = 0;
    job.thinLineIsHidden = YES;
    job.thickLineIsHidden = NO;
    job.cellHeight = 44 + 15 + 2.5;
    job.handler = ^{
    };
    [cellDataSource addObjectsFromArray:@[age,Height,type,constellation,income,job]];
    return cellDataSource;
}

-(NSMutableArray<PersonalCellModel *> *)bottomCellDataSource {
    NSMutableArray *cellDataSource = [[NSMutableArray alloc] init];
    PersonalCellModel *phone = [[PersonalCellModel alloc] init];
    phone.title = NSLocalizedString(@"profile7", nil);
    phone.icon = @"all_personal_phone";
    phone.info = self.model.mobile;
    phone.tag = 0;
    phone.thinLineIsHidden = NO;
    phone.thickLineIsHidden = YES;
    phone.cellHeight = 44;
    
    PersonalCellModel *faceBook = [[PersonalCellModel alloc] init];
    faceBook.title = NSLocalizedString(@"profile8", nil);
    faceBook.icon = @"all_personal_facebook";
    faceBook.info = self.model.facebook;
    faceBook.tag = 1;
    faceBook.thinLineIsHidden = NO;
    faceBook.thickLineIsHidden = YES;
    faceBook.cellHeight = 44;
    
    PersonalCellModel *whatsApp = [[PersonalCellModel alloc] init];
    whatsApp.title = NSLocalizedString(@"profile9", nil);
    whatsApp.icon = @"all_personal_whatsapp";
    whatsApp.info = self.model.whatApp;
    whatsApp.tag = 2;
    whatsApp.thinLineIsHidden = NO;
    whatsApp.thickLineIsHidden = YES;
    whatsApp.cellHeight = 44;
    
    PersonalCellModel *other = [[PersonalCellModel alloc] init];
    other.title = NSLocalizedString(@"profile10", nil);
    other.icon = @"all_personal_other";
    other.info = self.model.other;
    other.tag = 3;
    other.thinLineIsHidden = NO;
    other.thickLineIsHidden = YES;
    other.cellHeight = 44;
    
    [cellDataSource addObjectsFromArray:@[phone,faceBook,whatsApp,other]];
    return cellDataSource;
}

- (NSString *)HOTCHATengUserHeightWith:(NSString *)maxFeet minFeet:(NSString *)minFeet {
    NSString *heightStr = @"";
    if ([NSString stringWithFormat:@"%@",minFeet].length == 0 || [NSString stringWithFormat:@"%@",maxFeet].length == 0 || !minFeet || !maxFeet) {
        
        heightStr = @"";
    }else{
        heightStr = [NSString stringWithFormat:@"%@’%@”",minFeet,maxFeet];
    }
    return heightStr;
}
@end
